package com.ordering.dao.product.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.ordering.constant.ResultResponse;
import com.ordering.dao.product.ProductDao;
import com.ordering.entity.Order;
import com.ordering.entity.Page;
import com.ordering.entity.Product;
import com.ordering.util.DateAndTime;
import com.ordering.util.HibernateUtil;

public class ProductDaoImpl implements ProductDao
{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Override
    public ResultResponse add(Product product)
    {
        Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        try{
            s=HibernateUtil.getSession();
            
            Transaction tran = s.beginTransaction();
            s.save(product);
            tran.commit();
            resultResponse.setResult(true);
        }catch (Exception e) {
            resultResponse.setMessage(e.getMessage());
            e.printStackTrace();
        }finally{
            if(s!=null)
                s.close();
        }
        return resultResponse;
    }

    @Override
    public ResultResponse delect(Product product)
    {
        ResultResponse resultResponse = new ResultResponse();
        Session s=null;
        try
        {
            s=HibernateUtil.getSession();
            Transaction tran = s.beginTransaction();
            s.delete(product);
            tran.commit();
            resultResponse.setResult(true);
        }
        catch (Exception e)
        {
            resultResponse.setResult(false);
            resultResponse.setMessage(e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            if(s!=null)
                s.close();
        }
        return resultResponse;
    }

    @Override
    public ResultResponse get4page(Product product,Page page)
    {
        Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        s=HibernateUtil.getSession();
        Query q = s.createQuery("from Product where status = 2 order by create_time desc ");
        q.setFirstResult(page.getBegin());//设置起始行
        q.setMaxResults(page.getPageSize());//每页条数
        List cats = q.list(); //得到每页的数据
        if(s != null)
        s.close();
        resultResponse.setResult(cats);
        return resultResponse;
    }
	@Override
	public ResultResponse count(Product product) {
		Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        s=HibernateUtil.getSession();
        Query q = s.createQuery("select count(*) from Product where status = 2 ");
        resultResponse.setResult(((Number)q.uniqueResult()).longValue());
        
		return resultResponse;
	}
	@Override
	public Product getById(long productId) {
		Session s=null;
        s=HibernateUtil.getSession();
        Query q = s.createQuery("from Product where id = ? ");
        q.setLong(0, productId);
        List<Product> list = q.list();
		return list==null?null:list.get(0);
		
	}
	@Override
	public boolean updateStatus(long id, int status) {
		Session s=null;
        s=HibernateUtil.getSession();
        StringBuffer sb = new StringBuffer("update Product set status = ?  where id= ? ");
        
        Query q = s.createQuery(sb.toString());
        
        //插入状态
        q.setInteger(0, status);
        
        q.setLong(1, id);
        
        	
        boolean result = q.executeUpdate() > 0;
        
        if(s != null)
        s.close();
        return result;
	}
	@Override
	public ResultResponse update(Product product) {
		Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        try{
            s=HibernateUtil.getSession();
            
            Transaction tran = s.beginTransaction();
            s.update(product);
            tran.commit();
            resultResponse.setResult(true);
        }catch (Exception e) {
            resultResponse.setMessage(e.getMessage());
            e.printStackTrace();
        }finally{
            if(s!=null)
                s.close();
        }
        return resultResponse;
	}

}
